package com.yangxd.hospital.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxd.hospital.entity.Department;
import com.yangxd.hospital.entity.Registration;
import com.yangxd.hospital.service.RegistrationService;
import com.yangxd.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangxd
 * @since 2021-05-07
 */
@RestController
@RequestMapping("/hospital/registration")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    @GetMapping("/getHistory/{userId}")
    @ApiOperation(value = "所有的预约记录",notes = "根据userId查询所有记录")
    public Result getHisList(@PathVariable Integer userId) {
        List<Registration> registrationList = new ArrayList<>();
        registrationList = registrationService.getHisById(userId);
        return Result.ok().data("historyList",registrationList);
    }

    @PostMapping("/saveRegistration")
    @ApiOperation(value = "保存挂号记录",notes = "用户挂号")
    public Result saveReg(@RequestBody(required = true) Registration registration) {

        registrationService.saveRegistration(registration);
        return Result.ok().message("支付成功！预约请求正在处理中");

    }

    @GetMapping("/getManage/{doctorId}")
    @ApiOperation(value = "我的预约",notes = "通过医生ID获取")
    public Result getManage(@PathVariable Integer doctorId) {
        List<Registration> registrationList = new ArrayList<>();
        registrationList = registrationService.getManById(doctorId);
        return Result.ok().data("registrationList",registrationList);
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "更改预约状态",notes = "根据status修改")
    public Result updateStatus(@RequestParam Integer status,@RequestParam Integer registrationId) {
        registrationService.updateStatus(status,registrationId);
        return Result.ok().message("更改成功！");
    }

    @PostMapping("/getAll")
    @ApiOperation(value = "获取所有预约记录")
    public Result getAllReg(@RequestBody Registration registration,
                            @RequestParam(required = true,defaultValue = "1") Integer current,
                            @RequestParam(required = true,defaultValue = "6") Integer size) {

        Page<Registration> page = new Page<>(current,size);
        QueryWrapper<Registration> queryWrapper = getQueryWrapper(registration);
        IPage<Registration> registrationIPage = registrationService.findPageRegistration(page, queryWrapper);
        long total = registrationIPage.getTotal();
        List<Registration> registrationList = registrationIPage.getRecords();
        if(null != registrationList && registrationList.size() > 0) {
            return Result.ok().data("total",total).data("registrationList",registrationList);
        } else {
            return Result.error();
        }

    }

    @GetMapping("/getNum/{doctorId}")
    @ApiOperation(value = "获取医生当前预约人数")
    public Result getNum(@PathVariable Integer doctorId) {
        int count =  registrationService.selectNum(doctorId);
        return Result.ok().data("count",count);

    }

    private QueryWrapper<Registration> getQueryWrapper(Registration registration) {
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();
        if (registration != null) {
            if (!StringUtils.isEmpty(registration.getStatus())) {
                queryWrapper.eq("status", registration.getStatus());
            }
            if (!StringUtils.isEmpty(registration.getUserName())) {
                queryWrapper.eq("user_name", registration.getUserName());
            }
            if (!StringUtils.isEmpty(registration.getDepartmentName())) {
                queryWrapper.eq("department_name", registration.getDepartmentName());
            }
            if (!StringUtils.isEmpty(registration.getDoctorName())) {
                queryWrapper.eq("doctor_name", registration.getDoctorName());
            }
//            queryWrapper.eq("deleted",0);

        }
        return queryWrapper;

    }



}

